<?php

namespace App\Api;

use App\Exports\LogExport;
use App\Exports\LogsExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
use App\Models\Log;
use App\Models\User;
use Illuminate\Http\Request;

class LogController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
//        分页查询
        //获取获取到[user_name,user_mobile,user_id]，有或者没有when
        $users = User::when($request->user_name, function ($query) use ($request) {
            return $query->where("name", "like", "%{$request->user_name}%");
        })->when($request->user_mobile, function ($query) use ($request) {
            return $query->where("mobile", "like", "%{$request->user_mobile}%");
        })->when($request->user_id, function ($query) use ($request) {
            return $query->where("id", "=", $request->user_id);
        })->get();
        $user_ids = $users->pluck("id");
        return $this->success("",Log::whereIn("user_id", $user_ids)->latest()->paginate(10));
    }

//    导出
    public function export(Request $request)
    {
        return Excel::download(new LogsExport, 'logs.xlsx');
    }
}
